package org.zht.dormpro.dorm.service.impl;

import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.bean.DormDTO;
import org.zht.dormpro.dorm.cacher.IDormCacher;
import org.zht.dormpro.dorm.mapper.TbDormMapper;
import org.zht.dormpro.dorm.model.TbDorm;
import org.zht.dormpro.dorm.service.IDormService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DormServiceImpl implements IDormService {

    @Autowired
    private TbDormMapper dormMapper;
    @Autowired
    private IDormCacher dormCacher;

    @Override
    public List<DormDTO> getDormByCondition(PageBean pageBean) {
        return dormMapper.getAllDormByCondition(pageBean);
    }

    @Override
    public boolean addDorm(DormDTO dormDTO) {
        TbDorm dorm = new TbDorm();
        BeanUtils.copyProperties(dormDTO,dorm);
        int i = dormMapper.insertSelective(dorm);
        return i > 0;
    }

    @Override
    public TbDorm getDormById(Integer id) {
        return dormMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean editDorm(DormDTO dormDTO) {
        TbDorm dorm = new TbDorm();
        BeanUtils.copyProperties(dormDTO,dorm);
        int i = dormMapper.updateByPrimaryKeySelective(dorm);
        return i > 0;
    }

    @Override
    public boolean deleteDorm(Integer id) {
        return dormMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean batchRemoveDorm(ArrayList<String> dormIds) {
        if(CollectionUtil.isNotEmpty(dormIds)){
            for (String dormId:dormIds) {
                boolean b = deleteDorm(Integer.valueOf(dormId));
                if(!b){
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public List<HashMap<String, Object>> getAllBed(String dormId) {
        List<HashMap<String, Object>> beds = dormCacher.getAllBed(dormId);
        return beds;
    }

    @Override
    public List<TbDorm> getDormByFloorId(String floorId) {
        //TODO 可以使用缓存
        return dormCacher.getDormByFloorId(floorId);
    }
}
